package com.scientificInfo.server;

import com.scientificInfo.bean.*;
import com.scientificInfo.dao.LoginCountMapper;
import com.scientificInfo.dao.MessageMapper;
import com.scientificInfo.dao.UserMapper;
import com.scientificInfo.util.DateUtil;
//import javafx.scene.shape.Circle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServer {
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    LoginCountMapper loginCountMapper;

    /**
     * 教师登陆时，查询系统私信消息。
     *
     * @param teacherId 登陆教师Id
     * @return 消息list
     */
    public List<Message> selectMsgToTeacher(String teacherId) {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria circle = messageExample.createCriteria();
        circle.andTouserEqualTo(teacherId);
        circle.andEndGreaterThan(DateUtil.YMDToNum());
        return messageMapper.selectByExample(messageExample);
    }

    public List<Message> selectMsgToAllTeacher() {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria circle = messageExample.createCriteria();
        circle.andTouserEqualTo("allTeacher");
        circle.andEndGreaterThan(DateUtil.YMDToNum());
        return messageMapper.selectByExample(messageExample);
    }


    /**
     * 检查是否存在这个用户名
     *
     * @param userId 要查询的 userId
     * @return 如果存在返回 true ,否则返回 false
     */
    public boolean checkUser(String userId) {
        UserExample userExample = new UserExample();
        UserExample.Criteria circle = userExample.createCriteria();
        circle.andUserIdEqualTo(userId);
        return userMapper.selectByExample(userExample).size() == 0 ? false : true;
    }

    public User getUserById(User user) {
        return userMapper.selectByPrimaryKey(user.getUserId());
    }

    /*****
     * 修改管理员的密码
     */

    public int updatePassword(String userId, String password) {
        User user = new User();
        user.setUserId(userId);
        user.setUserPaswd(password);

        System.out.println(user);
        return userMapper.updateById(user);
    }

    @Transactional
    public int checkUserCountNum(User user) {
        int code = 1;
        try {
            LoginCount loginCountSelect = new LoginCount();
            loginCountSelect.setUserId(user.getUserId());
            //根据这个userId查到对应的信息 包括用户名与登录次数
            loginCountSelect = loginCountMapper.selectOne(loginCountSelect);

            //如果第一次登录 就置为1
            if (loginCountSelect == null) {
                LoginCount loginCountInsert = new LoginCount();
                loginCountInsert.setUserId(user.getUserId());
                loginCountMapper.insertOne(loginCountInsert, 1);
            } else {
                //否则 根据这个信息对象 更改为+1
                loginCountMapper.updateOne(loginCountSelect, loginCountSelect.getLoginCount() + 1);
            }
        } catch (Exception e) {
            code = 0;
            return code;
        }
        return code;
    }
}
